﻿namespace oneforty_purchase
{
    public struct oneforty_urls
    {
        public const string sandboxbase = "https://sandbox.oneforty.com/";
        public const string apibase = "https://api.oneforty.com/";
        public const string fulfillment_acknowledge = "fulfillment/acknowledge";
        public const string fulfillment_complete = "fulfillment/complete";
    }

    public struct OneFortyAction
    {
        public const string fulfillment_notification = "fulfillment_notification";
        public const string fulfillment_void = "fulfillment_void";
        public const string fulfillment_acknowledge = "fulfillment_acknowledge";
        public const string fulfillment_complete = "fulfillment_complete";
    }

    public class OneFortyPurchase
    {
        private bool _test;
        private string _action, _developer_key, _reference_code, _edition_code, _buyer_twitter_handle, _buyer_twitter_id, _buyer_email;
        private string _error, _license_key, _request;

        public bool test
        {
            get { return _test; }
            set { _test = value; }
        }

        public string action
        {
            get { return _action; }
            set { _action = value; }
        }

        public string developer_key
        {
            get { return _developer_key; }
            set { _developer_key = value; }
        }

        public string reference_code
        {
            get { return _reference_code; }
            set { _reference_code = value; }
        }

        public string edition_code
        {
            get { return _edition_code; }
            set { _edition_code = value; }
        }

        public string buyer_twitter_handle
        {
            get { return _buyer_twitter_handle; }
            set { _buyer_twitter_handle = value; }
        }

        public string buyer_twitter_id
        {
            get { return _buyer_twitter_id; }
            set { _buyer_twitter_id = value; }
        }

        public string buyer_email
        {
            get { return _buyer_email; }
            set { _buyer_email = value; }
        }

        public string error
        {
            get { return _error; }
            set { _error = value; }
        }

        public string license_key
        {
            get { return _license_key; }
            set { _license_key = value; }
        }

        public string request
        {
            get { return _request; }
            set { _request = value; }
        }


        public void Load(string body)
        {
            //Simple JSON string parsing.
            //Will need to be updated in the future if the spec becomes more complicated.
            //If using .NET 3.5, there is a builtin parser.  JSON.NET is also an option.
            //Decided to keep it simple for this version.
            if (body.Substring(0, 1) == "{" && body.Substring(body.Length - 1, 1) == "}")
            {
                //Remove outer braces.
                string strJson = body.Substring(1, body.Length - 2);
                //Remove double quotes.
                //Note:  Data items should not contain double quotes, but need to monitor spec.
                strJson = strJson.Replace("\"", "");
                //Split key/value pairs on the comma.
                //Note:  Data items should not contain commas, but need to monitor spec.
                string[] pairs = strJson.Split(',');
                //Load this object using key/value pairs.
                foreach (string pair in pairs)
                {
                    //Split key and value on the colon.
                    //Note:  Data items should not contain colons, but need to monitor spec.
                    string[] keyvalue = pair.Split(':');
                    if (keyvalue.Length == 2)
                    {
                        string key = keyvalue[0].ToString().Trim();
                        string value = keyvalue[1].ToString().Trim();
                        switch (key)
                        {
                            case "test":
                                this.test = bool.Parse(value);
                                break;
                            case "action":
                                this.action = value;
                                break;
                            case "developer_key":
                                this.developer_key = value;
                                break;
                            case "reference_code":
                                this.reference_code = value;
                                break;
                            case "edition_code":
                                this.edition_code = value;
                                break;
                            case "buyer_twitter_handle":
                                this.buyer_twitter_handle = value;
                                break;
                            case "buyer_twitter_id":
                                this.buyer_twitter_id = value;
                                break;
                            case "buyer_email":
                                this.buyer_email = value;
                                break;
                            case "error":
                                this.error = value;
                                break;
                            case "license_key":
                                this.license_key = value;
                                break;
                            case "request":
                                this.request = value;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
        public string ToJsonString()
        {
            string json = "";
            json = JsonKeyValue(json, "test", this.test.ToString().ToLower());
            json = JsonKeyValue(json, "action", this.action);
            json = JsonKeyValue(json, "developer_key", this.developer_key);
            json = JsonKeyValue(json, "reference_code", this.reference_code);
            json = JsonKeyValue(json, "edition_code", this.edition_code);
            json = JsonKeyValue(json, "buyer_twitter_handle", this.buyer_twitter_handle);
            json = JsonKeyValue(json, "buyer_twitter_id", this.buyer_twitter_id);
            json = JsonKeyValue(json, "buyer_email", this.buyer_email);
            json = JsonKeyValue(json, "error", this.error);
            json = JsonKeyValue(json, "license_key", this.license_key);
            json = JsonKeyValue(json, "request", this.request);

            if (json.Length > 0)
            {
                json = "{" + json + "}";
            }

            return json;
        }
        public string JsonKeyValue(string json, string key, string value)
        {
            if (key != null && value != null)
            {
                if (json.Length > 0)
                {
                    json += ",";
                }
                json += "\"" + key + "\": ";
                if (value == "true" || value == "false")
                {
                    json += value;
                }
                else
                {
                    json += "\"" + value + "\"";
                }
            }
            return json;
        }
    }
}
